﻿using AutoMapper;
using B.S.DataBase.MyProject.API.Appicant.Command;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.UserCommandHandler
{
    public class OnenessUserNameCommandHandler : IRequestHandler<OnenessUserNameCommand, APIResult<object>>
    {


        private readonly IBaseRepository<User> userrReppos;
        private readonly IMapper mapper;

        public OnenessUserNameCommandHandler(IBaseRepository<User> userrReppos, IMapper mapper)
        {
            this.userrReppos = userrReppos;
            this.mapper = mapper;
        }

        /// <summary>
        /// 唯一性
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<APIResult<object>> Handle(OnenessUserNameCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            var user = userrReppos.GetAll().Result.Where(x => x.UserName == request.UserName).ToList();
            if (user.Count() > 0)
            {
                result.Code = APIEnum.用户名重复;
                result.Msg = APIEnum.用户名重复.ToString();
            }
            else
            {
                result.Code = APIEnum.用户名唯一通过;
                result.Msg = APIEnum.用户名唯一通过.ToString();
            }
            return Task.FromResult(result);
        }
    }
}
